package com.mew.user.service;

import com.mew.common.dc.db.UserService;
import com.mew.common.dc.mapper.UserMapper;
import com.mew.common.enm.GoldProcessType;
import com.mew.common.enm.MailAccessoryType;
import com.mew.common.entity.mail.Accessory;
import com.mew.common.entity.mail.Mail;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.user.SimpleUserBO;
import com.mew.mail.service.MailService;
import com.mew.user.config.EventsLastingConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author dylin
 * @create 2021/3/31
 */
@Service
@Slf4j
public class InviteService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private MailService mailDBService;
    @Autowired
    private EventsLastingConfig eventsLastingConfig;
     /**
     * 绑定推荐人
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int bindInviter(long uid,String inv){
        User invUser = userService.getUserByGid(inv);
        if(invUser==null){
            return ResponseCode.UserNotExist.code();
        }
        if(uid==invUser.getId()){
            return ResponseCode.notBindOneself.code();
        }
        User user = userService.getUserById(uid);
        if(user.getInviter()!=null&&user.getInviter()>0){
            return ResponseCode.hasInviter.code();
        }

        int n=getInviteUserList(invUser.getId()).size();
        if(n<20){
            User dbUser=User.builder().id(uid).inviter(invUser.getId()).build();
            userInfoService.updateByID(dbUser);
            //yo邮件一封
            Mail mail=Mail.builder()
                    .type(GoldProcessType.Invite.code())
                    .title("รางวัลการเชิญ")
                    .content("ยินดีด้วย คุณเชิญผู้เล่น "+invUser.getNickname()+" สำเร็จ ได้รับรางวัลการเชิญ"+eventsLastingConfig.getInvite()+"เหรียญทอง")
                    .sendUser(-1l)
                    .sendTime(new Date())
                    .receiveUser(invUser.getId())
                    .status(0)
                    .build();
            Accessory accessory=Accessory.builder()
                    .type(MailAccessoryType.Gold.code())
                    .quantity(eventsLastingConfig.getInvite())
                    .build();
            mailDBService.insertMail(mail,accessory);
            //yo邮件一封
            Mail mail2=Mail.builder()
                    .type(GoldProcessType.Invite.code())
                    .title("รางวัลการรับเชิญ")
                    .content("ยินดีด้วย คุณรับการเชิญของ"+user.getNickname()+"สำเร็จ ได้รับรางวัลการรับเชิญ"+eventsLastingConfig.getBindInviter()+"เหรียญทอง")
                    .sendUser(-1l)
                    .sendTime(new Date())
                    .receiveUser(uid)
                    .status(0)
                    .build();
            Accessory accessory2=Accessory.builder()
                    .type(MailAccessoryType.Gold.code())
                    .quantity(eventsLastingConfig.getBindInviter())
                    .build();
            mailDBService.insertMail(mail2,accessory2);

            return ResponseCode.Success.code();
        }else{
            return ResponseCode.bindInviteMax.code();
        }
    }

    /**
     * 获取推荐用户列表
     * @param userId
     * @return
     */
    public List<SimpleUserBO> getInviteUserList(long userId){
        return userMapper.getInviteUserByUserId(userId);
    }
}
